package com.pxx.pj.common.config;

import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.MemoryConstrainedCacheManager;
import org.apache.shiro.mgt.RememberMeManager;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Configuration//这里的配置都可以写到启动类
public class SpringShiroConfig {
    //配置登录首页用户名
    @Bean
    public SessionManager sessionManager() {
        DefaultWebSessionManager sManager=
                new DefaultWebSessionManager();
        sManager.setGlobalSessionTimeout(60*60*1000);
        return sManager;
    }

    //配置记住我
    @Bean
    public RememberMeManager rememberMeManager() {
        CookieRememberMeManager cManager=
                new CookieRememberMeManager();
        SimpleCookie cookie=new SimpleCookie("rememberMe");
        cookie.setMaxAge(7*24*60*60);
        cManager.setCookie(cookie);
        return cManager;
    }

    //配置缓存
    @Bean
    public CacheManager shiroCacheManager(){
        return new MemoryConstrainedCacheManager();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor au = new AuthorizationAttributeSourceAdvisor();
        au.setSecurityManager(securityManager);
        return au;
    }


    @Bean
    public SecurityManager securityManager(Realm realm,
                                           CacheManager cacheManager,
                                           RememberMeManager rememberManager,
                                           SessionManager sessionManager
                                           ) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(realm);
        defaultWebSecurityManager.setCacheManager(cacheManager);
        defaultWebSecurityManager.setRememberMeManager(rememberManager);
        defaultWebSecurityManager.setSessionManager(sessionManager);
        return defaultWebSecurityManager;
    }

    @Bean// 设置访问规则
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("/doLoginUI");
        Map<String, String> fillMap = new LinkedHashMap<>();
//静态资源允许匿名访问:"anon"
        fillMap.put("/bower_components/**", "anon");
        fillMap.put("/modules/**", "anon");
        fillMap.put("/dist/**", "anon");
        fillMap.put("/plugins/**", "anon");
        fillMap.put("/user/doLogin", "anon");
        fillMap.put("/doLogout", "logout");
        //除了匿名访问的资源,其它都要认证("authc")后访问
        fillMap.put("/**", "user");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(fillMap);
        return shiroFilterFactoryBean;
    }
}

